package com.jinsilu.freight.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.TypedValue;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.hjq.http.EasyHttp;
import com.hjq.http.exception.ResultException;
import com.hjq.http.listener.HttpCallback;
import com.jinsilu.freight.R;
import com.jinsilu.freight.aop.CheckNet;
import com.jinsilu.freight.aop.DebugLog;
import com.jinsilu.freight.common.MyActivity;
import com.jinsilu.freight.helper.CommonHelper;
import com.jinsilu.freight.http.model.HttpListData;
import com.jinsilu.freight.http.request.GetBlankNoteListApi;
import com.jinsilu.freight.http.response.RecordWhiteNoteBean;
import com.jinsilu.freight.ui.adapter.BlankNoteAdapter;
import com.jinsilu.freight.ui.adapter.BlankNoteConsumeDetailsAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * author : byd
 * time   : 2022-04-08
 * desc   : 白条列表页面
 */
public final class BlankNoteConsumeDetailsActivity extends MyActivity {

    public static final String INTENT_KEY_IN_STATUS = "white_note_status";

    @CheckNet
    @DebugLog
    public static void start(Context context, String status) {
        if (TextUtils.isEmpty(status)) {
            return;
        }
        Intent intent = new Intent(context, BlankNoteConsumeDetailsActivity.class);
        intent.putExtra(INTENT_KEY_IN_STATUS, status);
        context.startActivity(intent);
    }

    private SmartRefreshLayout refresh;
    private RecyclerView recycler;
    private BlankNoteConsumeDetailsAdapter mAdapter;

    private Integer mCurrentPage = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_blank_note_consume_details;
    }

    @Override
    protected void initView() {
        recycler = findViewById(R.id.recycler);
        refresh = findViewById(R.id.refresh);

        refresh.setEnableLoadMore(false);

        recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerDecoration.builder(this)
                .asSpace().size(6, TypedValue.COMPLEX_UNIT_DIP)
                .showSideDividers()
                .showFirstDivider()
                .showLastDivider()
                .build()
                .addTo(recycler);

        mAdapter = new BlankNoteConsumeDetailsAdapter(new ArrayList<>());

        recycler.setAdapter(mAdapter);

        refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mCurrentPage += 1;
                getBlankNoteList();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mCurrentPage = 1;
                getBlankNoteList();
            }
        });
    }

    private void getBlankNoteList() {
        // 获取白条列表
        EasyHttp.get(this)
                .api(new GetBlankNoteListApi().setPageNum(String.valueOf(mCurrentPage)).setPageSize("10")
                        .setBusinessType(getString(INTENT_KEY_IN_STATUS)))
                .request(new HttpCallback<HttpListData<RecordWhiteNoteBean>>(this) {
                    @Override
                    public void onSucceed(HttpListData<RecordWhiteNoteBean> data) {
                        CommonHelper.finishedLoading(refresh);
                        List<RecordWhiteNoteBean> sourceHallBeans = data.getData();
                        if (sourceHallBeans.isEmpty()) {
                            mAdapter.setEmptyView(R.layout.layout_empty_data_view);
                            refresh.setEnableLoadMore(false);
                        }

                        refresh.setEnableLoadMore(sourceHallBeans.size() >= 10);

                        if (mCurrentPage == 1)
                            mAdapter.setList(sourceHallBeans);
                        else
                            mAdapter.addData(sourceHallBeans);
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        CommonHelper.finishedLoading(refresh);
                        if (e instanceof ResultException) {
                            if (e.getMessage().equals("暂无数据")) {
                                mAdapter.getData().clear();
                                mAdapter.notifyDataSetChanged();
                                mAdapter.setEmptyView(R.layout.layout_empty_data_view);
                                refresh.setEnableLoadMore(false);
                            }
                        }
                    }
                });
    }

    @Override
    protected void initData() {
        getBlankNoteList();
    }
}